import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/show")
public class ShowRequest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       StringBuilder stringBuilder=new StringBuilder();
        //返回请求协议的名称和版本号
        stringBuilder.append(req.getProtocol());
        stringBuilder.append("<br>");
        //返回请求的Http方法的名称
        stringBuilder.append(req.getMethod());
        stringBuilder.append("<br>");
        //返回URL
        stringBuilder.append(req.getRequestURI());
        stringBuilder.append("<br>");
        //返回请求上下文的请求URL部分
       stringBuilder.append(req.getContextPath());
       stringBuilder.append("<br>");
       //返回请求中的查询字符串
        stringBuilder.append(req.getQueryString());
        stringBuilder.append("<br>");
        //以字符串形似返回指定的请求头的值
        stringBuilder.append("<h3> 请求头部分</h3>");
        Enumeration<String> enumeration=req.getHeaderNames();
        while(enumeration.hasMoreElements()){
            String HeadName=enumeration.nextElement();
            String HeadValue=req.getHeader(HeadName);
            stringBuilder.append(HeadName+":"+HeadValue+"<br>");
        }
        //设置响应类型
        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write(stringBuilder.toString());
        //假设查询字符串是name=zhangsan &password=123
        String name=req.getParameter("name");
        String password=req.getParameter("password");
        resp.getWriter().write("name:"+name+"<br>password:"+password);
    }
}
